package com.fire.control.system.module.file.controller.app;

import java.util.ArrayList;
import java.util.List;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.fire.control.system.dto.app.AppFileVo;
import com.fire.control.system.entity.File;
import com.fire.control.system.module.file.service.AppFileService;
import com.fire.control.system.module.system.service.AppManagerService;
import com.fire.control.system.standard.BusinessInterfaceStandard;
import com.fire.control.system.statics.ResponseMessage;
import com.fire.control.system.support.protocol.app.AppRequestProtocol;
import com.fire.control.system.support.protocol.comman.ResponseJsonProtocol;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.util.FileUtil;

/**
 * 
 * @ClassName:  AppFileController
 * @Description:  app端文件管理控制器
 * @author:  xiechengguang
 * @date:  2017年6月6日 下午2:19:58
 *
 */
@RestController
@RequestMapping(value = "/file")
public class AppFileController implements BusinessInterfaceStandard<AppRequestProtocol, ResponseProtocol> {
	
	@Autowired
	private AppFileService appFileService;
	@Autowired
	private AppManagerService appManagerService;
	
	@RequestMapping(value="/uploadFile", method=RequestMethod.POST)
	public ResponseProtocol uploadFile(MultipartHttpServletRequest request) {
		ResponseProtocol respProtocol = new ResponseJsonProtocol();
		List<AppFileVo> list = new ArrayList<AppFileVo>();
		AppFileVo fileVo = null;
		List<File> files = FileUtil.uploadFile(request);
		for (File file : files) {
			appFileService.saveFile(file);
			fileVo = new AppFileVo();
			fileVo.setId(file.getId());
			fileVo.setUrl(file.getAccessAddress());
			list.add(fileVo);
		}
		if(!list.isEmpty()){
			respProtocol.setData(list);
			return respProtocol;
		}
		ResponseMessage.CODE_DEFINED.messageToProtocol(respProtocol).setMessage("上传失败");
		return respProtocol;
	}
	
	
	
	/**
	 * 
	 * @Title: downloadFile
	 * @Description: TODO(下载文件)
	 * @param request
	 * @return
	 * @return: ResponseProtocol
	 *@author:tanyuting
	 * @throws FileNotFoundException 
	 */
	 @RequestMapping(value="download/android",method=RequestMethod.GET)
	 public void downloadandroid(HttpServletResponse response) throws FileNotFoundException  {
		
		File file = appManagerService.getappdownloadFile();
		 // 下载本地文件
        String fileName = file.getName()+"."+file.getSuffix(); // 文件的默认保存名
        // 读到流中
        InputStream inStream = new FileInputStream(file.getPhysicalAddress());// 文件的存放路径
        // 设置输出的格式
        response.reset();
        response.setContentType("bin");
        response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        try {
            while ((len = inStream.read(b)) > 0)
            	response.getOutputStream().write(b, 0, len);
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
	
	 }
		
	

}
